﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MpSoft.Library.DAO.Categorys;
using MpSoft.Library.DTO;
using MpSoft.Basic.Configuration;
using MpSoft.Basic.Utils;

namespace MpSoft.Library.BUSS.Categories
{
    public class CategoryBUSS
    {
        #region Contain

        public const string CATEGORIES_ALL_KEY = "Mp.category.all-{0}-{1}";
        private const string CATEGORIES_ALL_MENU_MODULE_KEY = "Mp.category.all-{0}-{1}-{2}";
        private const string CATEGORIES_BY_ID_KEY = "Mp.category.id-{0}";

        private static void RemoveCache()
        {
            MpCache.RemoveByPattern(CATEGORIES_ALL_KEY);
            MpCache.RemoveByPattern(CATEGORIES_ALL_MENU_MODULE_KEY);
            MpCache.RemoveByPattern(CATEGORIES_BY_ID_KEY);
            MenuBUSS.RemoveCacheMenu();
        }

        #endregion

        public static int Category_Add(Category obj)
        {
            //obj.CategoryDESC = string.IsNullOrEmpty(obj.CategoryDESC) ? null : obj.CategoryDESC;
            //obj.MetaTitle = string.IsNullOrEmpty(obj.MetaTitle) ? null : obj.MetaTitle;
            //obj.MetaKeyword = string.IsNullOrEmpty(obj.MetaKeyword) ? null : obj.MetaKeyword;
            //obj.CategoryDESC = string.IsNullOrEmpty(obj.CategoryDESC) ? null : obj.CategoryDESC;
            var result = CategoryDAO.Category_Add(obj);
            if (result > 0)
                RemoveCache();
            return result;
        }
      
        public static int Category_Update(Category obj)
        {
            var result = CategoryDAO.Category_Update(obj);
            if (result > 0)
                RemoveCache();
            return result;
        }

        public static int Category_Delete(int categoryid)
        {
            var result = CategoryDAO.Category_Delete(categoryid);
            if (result > 0)
                RemoveCache();
            return result;
        }

        public static Category Category_GetByID(int categoryID)
        {
            return Category_GetByID(categoryID, null);
        }

        public static List<Category> Category_Menu(string Module, string menuid)
        {
            return Category_Menu(Module, menuid, 0);
        }

        public static List<Category> Category_Menu(string Module, string menuid, int IsActive)
        {
            return Category_ByParentID(-1, Module, menuid, IsActive);
        }

        public static List<Category> Category_ByParentID(int ParentID, string Module, string menuid, int IsActive)
        {
            string key = string.Format(CATEGORIES_ALL_MENU_MODULE_KEY, ParentID, Module, menuid + "ac" + IsActive);
            object obj = MpCache.Get(key);
            if (obj != null)
                return (List<Category>)obj;
            var listCate = Category_List(-1, ParentID, null, Module, menuid, IsActive, 1);
            if (listCate != null)
                MpCache.Max(key, listCate);
            return listCate;
        }

        public static string Category_ExistsSeName(string SeName)
        {
            var obj = Category_BySeName(SeName);
            int i = 1;
            while (obj != null)
            {
                SeName = SeName + "-" + i;
                obj = Category_BySeName(SeName);
                i++;
            }
            return SeName;
        }

        public static Category Category_BySeName(string SeName)
        {
            return Category_GetByID(-1, SeName);
        }

        public static Category Category_GetByID(int categryID, string SeName)
        {
            if (categryID <= 0 && string.IsNullOrEmpty(SeName))
                return null;
            string key = string.Format(CATEGORIES_BY_ID_KEY, (categryID > 0 ? categryID.ToString() : SeName));
            object obj = MpCache.Get(key);
            if (obj != null)
                return (Category)obj;
            var listCate = Category_List(categryID, -1, SeName, null, null, 0, -1);
            Category category = null;
            if (listCate != null)
            {
                category = listCate.FirstOrDefault();
                MpCache.Max(key, category);
            }
            return category;
        }

        public static List<Category> Category_Total(string TypeCode, int ParentID, int IsActive)
        {
            string key = string.Format(CATEGORIES_ALL_KEY, TypeCode, "p" + ParentID + "a" + IsActive);
            object obj = MpCache.Get(key);
            if (obj != null)
                return (List<Category>)obj;
            var listCate = CategoryDAO.Category_Total(TypeCode, ParentID, IsActive);
            if (listCate != null)
                MpCache.Max(key, listCate);
            return listCate;
        }

        public static List<Category> Category_List(int categryID, int parentID,
            string SeName, string typeCode, string MenuID, int IsActive, int SortOrder)
        {
            return CategoryDAO.Category_List(categryID, parentID, SeName, typeCode, MenuID, IsActive, SortOrder);
        }
    }
}
